# -*- coding: utf-8 -*-
"""
______________________________
  Author: 安智军
  Email :  1010666711@qq.com
   Time : 2023/8/6 11:28
    File: web_cookies.py
Software: PyCharm
______________________________
"""
import time
import yaml

from selenium import webdriver
from selenium.webdriver.common.by import By
from faker import Faker

class TestLogin:
    @classmethod
    # def setup(self):
    # def setup(cls):
    def setup_class(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(10)
        # self.driver = webdriver.Chrome()
        # self.driver.get("https://work.weixin.qq.com/wework_admin/frame")

        fake = Faker('zh_CN')
        cls.username = fake.name()
        cls.phone = fake.phone_number()
        cls. email = fake.email()
        # print(username)
    @classmethod
    def teardown_class(cls):
    # def teardown(cls):

        cls.driver.quit()
        # self.driver.quit()

    def test_get_login_cookies(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        time.sleep(15)
        cookies = self.driver.get_cookies()

        # 将获取到的 cookies 存放到本地的 yaml 文件中
        with open("cookies.yaml", "w") as s:
            yaml.safe_dump(data=cookies, stream=s)
        time.sleep(1)

    # def test_login2(self):
    #     # self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
    #     with open("cookies.yaml", "r") as s:
    #         cookies = yaml.safe_load(s)
    #     for cookie in cookies:
    #         self.driver.add_cookie(cookie)
    #     self.driver.refresh()
    #     time.sleep(1)
    #     # self.driver.find_element(By.XPATH,'//*[@id="menu_customer"]/span').click()
    #     # time.sleep(5)


    def test_add_member(self):


        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        with open("cookies.yaml", "r") as s:
            cookies = yaml.safe_load(s)
        for cookie in cookies:
            self.driver.add_cookie(cookie)
        self.driver.refresh()

        # 点击添加用户
        self.driver.find_element(By.XPATH,"//span[text()='添加成员']").click()

        #姓名
        self.driver.find_element(By.XPATH,'//input[@id="username"]').send_keys(self.username)
        # 账号
        # self.driver.find_element(By.XPATH,'//input[@id="memberAdd_acctid"]').send_keys(phone)
        self.driver.find_element(By.XPATH,'//*[@id="memberAdd_acctid"]').send_keys(self.phone)

        # 邮箱
        # self.driver.find_element(By.XPATH,'//input[@name="biz_mail"]').clear()
        # time.sleep(1)
        # self.driver.find_element(By.XPATH,'//input[@name="biz_mail"]').send_keys(email[0:5])
        # 手机号
        self.driver.find_element(By.XPATH,'//input[@class="qui_inputText ww_inputText ww_telInput_mainNumber"]').send_keys(self.phone)
        time.sleep(1)
        # 点击保存
        self.driver.find_element(By.XPATH,'//a[@class="qui_btn ww_btn js_btn_save"]').click()


        # 选择当前用户
        self.driver.find_element(By.XPATH, f'//*[text()="{self.username}"]/../../td/input').click()

        # 删除用户
        self.driver.find_element(By.XPATH, '//a[contains(@class,"qui_btn ww_btn js_delete")]').click()

        # 点击弹窗中的删除
        self.driver.find_element(By.XPATH, '//a[contains(@class,"qui_btn ww_btn ww_btn_Blue")]').click()
        time.sleep(10)


        time.sleep(15)

    # def test_delete_member(self):
    #     # self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
    #     with open("cookies.yaml", "r") as s:
    #         cookies = yaml.safe_load(s)
    #     for cookie in cookies:
    #         self.driver.add_cookie(cookie)
    #     self.driver.refresh()
    #
    #     # 点击添加用户
    #     self.driver.find_element(By.XPATH,"//span[text()='添加成员']").click()
    #
    #     #姓名
    #     self.driver.find_element(By.XPATH,'//input[@id="username"]').send_keys(self.username)
    #     # 账号
    #     self.driver.find_element(By.XPATH,'//*[@id="memberAdd_acctid"]').send_keys(self.phone)
    #
    #     # 手机号
    #     self.driver.find_element(By.XPATH,'//input[@class="qui_inputText ww_inputText ww_telInput_mainNumber"]').send_keys(self.phone)
    #     time.sleep(1)
    #     # 点击保存
    #     self.driver.find_element(By.XPATH,'//a[@class="qui_btn ww_btn js_btn_save"]').click()
    #
    #     # 跳转到通讯录
    #     self.driver.find_element(By.XPATH, '//a[@id="menu_contacts"]').click()
    #
    #     # 选择当前用户
    #     self.driver.find_element(By.XPATH, f'//*[text()="{self.username}"]/../../td/input').click()
    #
    #     # 删除用户
    #     self.driver.find_element(By.XPATH, '//a[contains(@class,"qui_btn ww_btn js_delete")]').click()
    #
    #     # 点击弹窗中的删除
    #     self.driver.find_element(By.XPATH, '//a[contains(@class,"qui_btn ww_btn ww_btn_Blue")]').click()
    #     time.sleep(10)
    #
    #
    #     time.sleep(10)





if __name__ == '__main__':
    test = TestLogin()
    # test.setup()
    test.setup_class()
    test.test_get_login_cookies()
    # test.test_login2()
    test.test_add_member()
    test.teardown_class()